package com.lk.api.app;

import android.app.Activity;
import android.app.AlertDialog;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.res.Configuration;
import android.net.Uri;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.lk.api.R;

public class FinishAffinity1 extends Activity {
    TextView textView;
    EditText editText;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onCreate(savedInstanceState);
        Log.d("FinishAffinity1_onCreate", "onCreate");
        Log.d("onCreate", "FinishAffinity1_TaskStack_TaskId: " + getTaskId());
        setContentView(R.layout.activity_finish_affinity1);

        textView = (TextView) findViewById(R.id.resulttextView);

        editText = (EditText) findViewById(R.id.saveText);

        Button button = (Button) findViewById(R.id.FinishAffinityButton);
        button.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                // 交换界面 并且传值到下个界面
                // Intent intent = new
                // Intent(FinishAffinity1.this,FinishAffinity2.class);
                Intent intent = new Intent();
                // 自定义Action
                intent.setAction("com.lk.api.lk");
                // 自定义scheme.设置http为前缀 和manifest里的http一致
                intent.setData(Uri.parse("http://www.widthbeam.com"));
                // 设置key 和 值
                intent.putExtra("com.lk.api.LK", "hello world");
                // 启动下一个activity 并且设置返回code码
                startActivityForResult(intent, 123);

            }
        });

        Button alertDialogButton = (Button) findViewById(R.id.alertdialog);
        alertDialogButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                final CharSequence[] items = { "Red", "Green", "Blue" };
                AlertDialog.Builder builder = new AlertDialog.Builder(
                        FinishAffinity1.this);
                builder.setTitle("Pick a color");

                builder.setItems(items, new DialogInterface.OnClickListener() {
                    @Override
                    public void onClick(DialogInterface dialog, int which) {
                        // TODO Auto-generated method stub

                        Toast.makeText(FinishAffinity1.this, items[which],
                                Toast.LENGTH_LONG).show();
                    }
                });

                AlertDialog alertDialog = builder.create();
                alertDialog.show();

            }
        });

        Button startActivityButton = (Button) findViewById(R.id.startActivity);
        startActivityButton.setOnClickListener(new OnClickListener() {

            @Override
            public void onClick(View v) {
                // TODO Auto-generated method stub
                Intent intent = new Intent(FinishAffinity1.this,
                        Occlusion.class);
                startActivity(intent);

            }
        });

        if (savedInstanceState != null) {
            Log.d("onCreate", "savedInstanceState");
            editText.setText(savedInstanceState.getString("lk"));
        }

    }

    // 恢复保存 如果程序意外终止 采用次方法
    // 恢复保存有两种方式一种是在onRestoreInstanceState恢复
    // 另外一种如下：
    // if (savedInstanceState != null) {
    // Log.d("onCreate", "savedInstanceState");
    // editText.setText(savedInstanceState.getString("lk"));
    // }
    @Override
    protected void onSaveInstanceState(Bundle outState) {
        // TODO Auto-generated method stub
        // super.onSaveInstanceState(outState);
        Log.d("FinishAffinity1_onSaveInstanceState", "onSaveInstanceState");
        outState.putString("lk", "lk" + editText.getText().toString());
        Log.d("outState", "" + outState.get("lk"));
    }

    @Override
    protected void onRestoreInstanceState(Bundle savedInstanceState) {
        // TODO Auto-generated method stub
        super.onRestoreInstanceState(savedInstanceState);
        Log.d("FinishAffinity1_onRestoreInstanceState",
                "onRestoreInstanceState");
//		if (savedInstanceState != null) {
//			editText.setText(savedInstanceState.getString("lk"));
//			Log.d("savedInstanceState", "" + savedInstanceState.getString("lk"));
//
//		}

    }

    //	这个方法用在：音乐播放 横屏竖屏改变的时候 音乐不会断。。。。
    @Override
    public void onConfigurationChanged(Configuration newConfig) {
        // TODO Auto-generated method stub
        super.onConfigurationChanged(newConfig);
        Log.d("FinishAffinity1_onConfigurationChanged",
                "onConfigurationChanged");

        // 判断是否是横屏 就打印log
        if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
            Log.d("ORIENTATION_LANDSCAPE", "ORIENTATION_LANDSCAPE");
        } else {
            Log.d("ORIENTATION_PORTRAIT", "ORIENTATION_PORTRAIT");
        }
    }

    @Override
    protected void onStart() {
        // TODO Auto-generated method stub
        Log.d("FinishAffinity1_onStart", "onStart");
        super.onStart();
    }

    @Override
    protected void onResume() {
        // TODO Auto-generated method stub
        Log.d("FinishAffinity1_onResume", "onResume");
        super.onResume();
    }

    @Override
    protected void onPause() {
        // TODO Auto-generated method stub
        Log.d("FinishAffinity1_onPause", "onPause");
        super.onPause();
    }

    @Override
    protected void onStop() {
        // TODO Auto-generated method stub
        Log.d("FinishAffinity1_onStop", "onStop");
        super.onStop();
    }

    @Override
    protected void onDestroy() {
        // TODO Auto-generated method stub
        Log.d("FinishAffinity1_onDestroy", "onDestroy");
        super.onDestroy();
    }

    @Override
    protected void onRestart() {
        // TODO Auto-generated method stub
        Log.d("FinishAffinity1_onRestart", "onRestart");
        super.onRestart();
    }

    // 接收返回值方法
    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        // TODO Auto-generated method stub
        // 判断返回的Code码 是不是 等于123
        if (resultCode == 123) {
            // receiveResultData
            String receiveResultData = data.getExtras().getString(
                    "com.lk.api.LK");
            // 设置textView显示
            textView.setText("Result :" + receiveResultData);

        }
        super.onActivityResult(requestCode, resultCode, data);
    }
}
